package com.mac.sys.urtils;

import java.text.DateFormat;
import java.text.DateFormatSymbols;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class TimeUtils
{
    public static String getCurrentTime()
    {
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        System.out.println("当前时间：" + sdf.format(d));
        return sdf.format(d);
    }

    public static String getCurrentDate()
    {
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//        System.out.println("当前时间：" + sdf.format(d));
        return sdf.format(d);
    }

    /**
     * @return yyyy-MM
     */
    public static String getCurrentMonth()
    {
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
//        System.out.println("当前时间：" + sdf.format(d));
        return sdf.format(d);
    }

    /**
     * @param date 2018-05-01/2018-05
     * @return m  5
     */
    public static Integer getMonthNo(String date)
    {
        String m = date.split("\\-")[1];
        return Integer.valueOf(m);
    }

    public static String getCurrentYear()
    {
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
//        System.out.println("当前时间：" + sdf.format(d));
        return sdf.format(d);
    }

    public static String getTime(Date date)
    {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }

    /*
     *pattern: "yyyy-MM-dd HH:mm:ss"
     */
    public static String getTime(Date date, String pattern)
    {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }

    public static String getMMDD(String YYYYMMDD)
    {
        String[] tmpArr = YYYYMMDD.split("\\-");
        return tmpArr[1] + "-" + tmpArr[2];
    }

    public static String getAfterDay(int n)
    {
        Calendar calendar = Calendar.getInstance();//此时打印它获取的是系统当前时间
        calendar.add(Calendar.DATE, n);    //得到前一天：-1,后2天： 2

        String afterDay = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
        return afterDay;
    }

    public static String getAfterDay(int n, String yyyyMMdd)
    {
        Calendar calendar = Calendar.getInstance();//此时打印它获取的是系统当前时间
        String[] timeArr = yyyyMMdd.split("\\-");
        calendar.set(Integer.parseInt(timeArr[0]), Integer.parseInt(timeArr[1]) - 1, Integer.parseInt(timeArr[2]));
        calendar.add(Calendar.DATE, n);    //得到前一天：-1,后2天： 2

        String afterDay = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
        return afterDay;
    }

    public static String getAfterMonth(int n)
    {
        Calendar calendar = Calendar.getInstance();//此时打印它获取的是系统当前时间
        calendar.add(Calendar.MONTH, n);    //得到前一天：-1,后2天： 2

        String afterDay = new SimpleDateFormat("yyyy-MM").format(calendar.getTime());
        return afterDay;
    }

    public static String getAfterMonth(int n, String yyyyMM)
    {
        Calendar calendar = Calendar.getInstance();//此时打印它获取的是系统当前时间
        String[] timeArr = yyyyMM.split("\\-");
        calendar.set(Integer.parseInt(timeArr[0]), Integer.parseInt(timeArr[1]) - 1, 1);
        calendar.add(Calendar.MONTH, n);    //得到前一天：-1,后2天： 2

        String afterDay = new SimpleDateFormat("yyyy-MM").format(calendar.getTime());
        return afterDay;
    }


    public static List<String> getAllYearDate(int year)
    {
        List<String> dateList = new ArrayList<String>();

        Calendar beginDay = new GregorianCalendar();
        Calendar endDay = new GregorianCalendar();
        DateFormatSymbols dfs = new DateFormatSymbols();
        String[] weeks = dfs.getWeekdays();
        beginDay.set(year, 0, 1); //Calendar的月从0-11，所以4月是3.		    
        endDay.set(year + 1, 0, 1); //Calendar的月从0-11，所以5月是4.		    
        int count = 1;
//        c_end.add(Calendar.DAY_OF_YEAR, 1); //结束日期下滚一天是为了包含最后一天		     		    
        while (beginDay.before(endDay))
        {
            Date date = new java.sql.Date(beginDay.getTime().getTime());
            dateList.add(date.toString());
            if (beginDay.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)
            {
                count++;
            }
            beginDay.add(Calendar.DAY_OF_YEAR, 1);
        }


        return dateList;
    }

    /**
     * 根据日期获取周几
     *
     * @param date 日期:yyyy-MM-dd
     * @return 1-7
     */
    public static int getWeekOfDayNo(String date)
    {
        String[] dateArr = date.split("\\-");
        Calendar cal = Calendar.getInstance();
        int year = Integer.valueOf(dateArr[0]);
        int month = Integer.valueOf(dateArr[1]) - 1;
        int day = Integer.valueOf(dateArr[2]);
        cal.set(year, month, day);

        int weekOfDayNo = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (weekOfDayNo == 0)
        {
            return 7;
        }
        return weekOfDayNo;

    }


    public static int getMonthDayNum(String date)
    {
        String[] dateArr = date.split("\\-");
        Calendar cal = Calendar.getInstance();
        int year = Integer.valueOf(dateArr[0]);
        int month = Integer.valueOf(dateArr[1]);
        cal.set(year, month, 0);

        return cal.get(Calendar.DATE);
    }

    /**
     * 根据日期获取周的第一日期
     *
     * @return
     */
    public static String getFirstDateOfWeek(long date)
    {
        Calendar calendar = Calendar.getInstance();
        calendar.setFirstDayOfWeek(Calendar.MONDAY);//设置星期一为一周开始的第一天
        calendar.setMinimalDaysInFirstWeek(4);//可以不用设置
        calendar.setTimeInMillis(date);//获得当前的时间戳
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);

        return getTime(calendar.getTime(), "yyyy-MM-dd");
    }

    /**
     * 根据日期获取周的第一日期
     *
     * @return
     */
    public static String getEndDateOfWeek(long date)
    {
        Calendar calendar = Calendar.getInstance();
        calendar.setFirstDayOfWeek(Calendar.MONDAY);//设置星期一为一周开始的第一天
        calendar.setMinimalDaysInFirstWeek(4);//可以不用设置
        calendar.setTimeInMillis(date);//获得当前的时间戳
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);

        return getTime(calendar.getTime(), "yyyy-MM-dd");
    }

    /**
     * 获取年周
     *
     * @param date
     * @return yyyy-W12
     */
    public static String getYearWeek(long date)
    {
        Calendar calendar = Calendar.getInstance();
        calendar.setFirstDayOfWeek(Calendar.MONDAY);//设置星期一为一周开始的第一天
        calendar.setMinimalDaysInFirstWeek(4);//可以不用设置
        calendar.setTimeInMillis(date);//获得当前的时间戳
        int year = calendar.get(Calendar.YEAR);//获得当前的年
        int weekOfYear = calendar.get(Calendar.WEEK_OF_YEAR);//获得当前日期属于今年的第几周
        if (weekOfYear < 10)
        {
            return year + "-W0" + weekOfYear;
        }

        return year + "-W" + weekOfYear;
    }

    /**
     * 获取年周
     *
     * @param date
     * @return yyyy-W12
     */
    public static String getYearWeek(String date)
    {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date dat = null;
        try
        {
            dat = format.parse(date);
        } catch (ParseException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Calendar calendar = Calendar.getInstance();
        calendar.setFirstDayOfWeek(Calendar.MONDAY);//设置星期一为一周开始的第一天
        calendar.setMinimalDaysInFirstWeek(4);//可以不用设置
        calendar.setTime(dat);//获得当前的时间戳
        int year = calendar.get(Calendar.YEAR);//获得当前的年
        int weekOfYear = calendar.get(Calendar.WEEK_OF_YEAR);//获得当前日期属于今年的第几周
        if (weekOfYear == 1)
        {
            year = year + 1;
        }
        return year + "-W" + weekOfYear;
    }

    private static Calendar getCalendarFormYear(int year)
    {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        cal.set(Calendar.YEAR, year);
        return cal;
    }

    public static String getStartDayOfWeekNo(int year, int weekNo)
    {
        Calendar cal = getCalendarFormYear(year);
        cal.set(Calendar.WEEK_OF_YEAR, weekNo);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(cal.getTime());
//        return cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-" +
//                cal.get(Calendar.DAY_OF_MONTH);

    }

    public static String getEndDayOfWeekNo(int year, int weekNo)
    {
        Calendar cal = getCalendarFormYear(year);
        cal.set(Calendar.WEEK_OF_YEAR, weekNo);
        cal.add(Calendar.DAY_OF_WEEK, 6);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(cal.getTime());
//        return cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-" +
//                cal.get(Calendar.DAY_OF_MONTH);
    }

    public static String getChineseWeekNo(String date)
    {
        int no = TimeUtils.getWeekOfDayNo(date);
        if (no == 1)
        {
            return "星期一";
        }
        if (no == 2)
        {
            return "星期二";
        }
        if (no == 3)
        {
            return "星期三";
        }
        if (no == 4)
        {
            return "星期四";
        }
        if (no == 5)
        {
            return "星期五";
        }
        if (no == 6)
        {
            return "星期六";
        }

        return "星期日";
    }

    // 获取当前时间所在年的最大周数
    public static String getMaxWeekNo(int year)
    {
        String yearWeek = getYearWeek(year + "-12-31");

        if (Integer.parseInt(yearWeek.split("-W")[0]) > year)
        {
            yearWeek = getYearWeek(year + "-12-24");
        }
        return yearWeek;
    }

    // 获取当前周列表，向前30
    public static List<String> getYearWeekList30()
    {
        String yearWeek = TimeUtils.getYearWeek(TimeUtils.getCurrentDate());

        String[] arr = yearWeek.split("-W");
        int year = Integer.parseInt(arr[0]);
        int week = Integer.parseInt(arr[1]);

        List<String> list = new ArrayList<String>();
        for (int i = 0; i < 30; i++)
        {
            if (week - i == 0)
            {
                year = year - 1;
                String preYearWeek = TimeUtils.getMaxWeekNo(year);
                String[] tmp = preYearWeek.split("-W");
                week = Integer.parseInt(tmp[1]) + i;
            }
            list.add(year + "-W" + (week - i));
        }
        Collections.reverse(list);
        return list;
    }


    //获取月最后一天
    public static String getLastDateOfMonth(String year, String month)
    {
        //year="2018" month="2"
        Calendar calendar = Calendar.getInstance();
        // 设置时间,当前时间不用设置
        calendar.set(Calendar.YEAR, Integer.parseInt(year));
        calendar.set(Calendar.MONTH, Integer.parseInt(month)-1);

        // System.out.println(calendar.getTime());

        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd ");
        return format.format(calendar.getTime());
    }

    public static void main(String[] args)
    {
//        List<String> list = TimeUtils.getYearWeekList30();
//
//        for (String str : list)
//        {
//            System.out.println(str);
//        }

        System.out.println(getLastDateOfMonth("2020","12"));
    }
}
